//
//  UserPresenter.m
//  EFExtend
//
//  Created by mac on 15/3/27.
//  Copyright (c) 2015年 Eric. All rights reserved.
//

#import "UserPresenter.h"
#import "UserApi.h"




@interface UserPresenter ()<APICallBackProtocol>
@property (nonatomic,HM_STRONG)UserApi *userApi;

@end

@implementation UserPresenter
@synthesize userApi;

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.userApi = [UserApi api];
    }
    return self;
}

- (void)dealloc
{
    self.userApi = nil;
    HM_SUPER_DEALLOC();
}

- (id)doSomething:(NSString *)something attributes:(NSArray *)attributeBinded{
    if ([something isEqual:@"Login"]) {
        [self doLogin:attributeBinded];
    }
    return nil;
}

- (void)fillThem:(NSArray *)attributeBinded forSomething:(NSString *)something{
    if ([something isEqual:@"Login"]) {
        [self fillLogin:attributeBinded];
    }
}

- (void)fillLogin:(NSArray *)attributeBinded{
    HMUITextField *username = [self getOutput:@"IOUsername"];
    HMUITextField *password = [self getOutput:@"IOPassword"];
    UIButten *autoLogin = [self getOutput:@"IOAutoLogin"];
    UIButten *rememberPW = [self getOutput:@"IORememberPW"];
    
    //api接口进行DB API数据存储
    //最后一个获取用户名
    //获取用户名密码
    //获取自动登录
    //获取记住密码
    UserBean *bean = [self.userApi getUserBean];
    [username setText:bean.userName];
    [password setText:bean.passWord];
    
}

- (void)doLogin:(NSArray *)attributeBinded{
    
    HMUITextField *username = [self getInput:@"IOUsername"];
    HMUITextField *password = [self getInput:@"IOPassword"];
    
    if (![username.text notEmpty]) {
        [self showFailureTip:@"温馨提示" detail:@"请输入用户名" timeOut:2.f];
        return;
    }
    
    if (![password.text notEmpty]) {
        [self showFailureTip:@"温馨提示" detail:@"请输入密码" timeOut:2.f];
        return;
    }
    
    //api接口进行REST API数据获取
    [self.userApi observeListener:self forCommand:@"LLJJJ"];
    [self.userApi login:@"LLJJJ" Username:username.text password:password.text];
    
}

- (void)api:(API*)api doSome:(NSString *)something bean:(id)bean state:(APIProcess)state userinfo:(id)userinfo error:(NSError *)error{
    
    INFO(@"back to Presenter",something,@(state),(bean==nil?@"no bean":bean),error);
    
    [self.delegate presenter:self doSome:something bean:bean state:(PresenterProcess)state error:error];
}
@end
